<style type="text/css">
<!--
div.pagination {
	margin: auto auto;
	display: table;
}

div.pagination a {
	padding: 2px 5px 2px 5px;
	margin: 2px;
	border: 1px solid #AAAADD;
	
	text-decoration: none; /* no underline */
	color: #000099;
}
div.pagination a:hover, div.pagination a:active {
	border: 1px solid #000099;

	color: #000;
}
div.pagination span.current {
	padding: 2px 5px 2px 5px;
	margin: 2px;
		border: 1px solid #000099;
		
		font-weight: bold;
		background-color: #000099;
		color: #FFF;
	}
	div.pagination span.disabled {
		padding: 2px 5px 2px 5px;
		margin: 2px;
		border: 1px solid #EEE;
	
		color: #DDD;
	}
	#nav {
	display: table;
	height:20px;
	margin: 0 auto;
	}
	#nav ul {margin:0px auto; padding:0px;}
	#nav ul li {display:inline; height:30px; float:left; list-style:none; margin-left: 5px;}
	#nav ul li a {color: #000000; text-decoration: none;}
	#nav ul li a:hover {color: #FF00FF; text-decoration: underline;}
	-->
</style>
<?php
	/*
		Place code to connect to your DB here.
	*/
		// include your code to connect to DB.

	$tbl_name=$conf['perfix']."template";		//your table name
	// How many adjacent pages should be shown on each side?
	$adjacents = 3;
	$queryString ="";
	/* 
	   First get total number of rows in data table. 
	   If you have a WHERE clause in your query, make sure you mirror it here.
	*/	
	
	/* Setup vars for query. */
	$targetpage = $_SERVER["PHP_SELF"]; 	//your file name  (the name of this file)
	$limit = 6; 								//how many items to show per page
	if(isset($_GET['page']))
		$page = $_GET['page'];
	else $page = null;
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0
	
	/* Get data. */
	$catalogid = "-1";
	if (isset($_GET['catalogid'])) {
	  $catalogid = $_GET['catalogid'];
	  $queryString = "&catalogid=".$catalogid;
	}
	if ($catalogid == "-1") 
	{
		$query="SELECT * FROM ".$conf['perfix']."template ORDER BY id DESC";
		$page_query = "SELECT COUNT(*) as num FROM $tbl_name";
		$total_pages = $DB->fetch_row($DB->query($page_query));
		$total_pages = $total_pages['num'];
	}
	else
	{
		$query = sprintf("SELECT * FROM ".$conf['perfix']."template WHERE catalogid=%s ORDER BY id DESC", $catalogid);
		$page_query = "SELECT COUNT(*) as num WHERE catalogid=%s FROM $tbl_name";
		$total_pages = $DB->fetch_row($DB->query($page_query));
		$total_pages = $total_pages['num'];
	}
	$sql = sprintf("%s LIMIT %d, %d", $query, $start, $limit);
	$result = $DB->query($sql);
	
	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//previous page is page - 1
	$next = $page + 1;							//next page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		//previous button
		if ($page > 1) 
			$pagination.= "<a href=\"$targetpage?page=$prev$queryString\">« previous</a>";
		else
			$pagination.= "<span class=\"disabled\">« previous</span>";	
		
		//pages	
		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?page=$counter$queryString\">$counter</a>";					
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
		{
			//close to beginning; only hide later pages
			if($page < 1 + ($adjacents * 2))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter$queryString\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1$queryString\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage$queryString\">$lastpage</a>";		
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "<a href=\"$targetpage?page=1$queryString\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2$queryString\">2</a>";
				$pagination.= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter$queryString\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1$queryString\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage$queryString\">$lastpage</a>";		
			}
			//close to end; only hide early pages
			else
			{
				$pagination.= "<a href=\"$targetpage?page=1$queryString\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2$queryString\">2</a>";
				$pagination.= "...";
				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter$queryString\">$counter</a>";					
				}
			}
		}
		
		//next button
		if ($page < $counter - 1) 
			$pagination.= "<a href=\"$targetpage?page=$next$queryString\">next »</a>";
		else
			$pagination.= "<span class=\"disabled\">next »</span>";
		$pagination.= "</div>\n";		
	}
?>
<div><table width="994" border="0" cellpadding="0" cellspacing="0" style="margin:0 auto">
<tr>
	<?php
		$tmpl = $DB->fetch_row($result);
		if(!$tmpl) echo "<td>No record(s) found.</td>";
		while($tmpl)
		{
			$folder = explode("-",$tmpl['adddate']);
			$path = $conf['rooturl']."images/templates/".$folder[0]."/".$folder[1]."/".$folder[2]."/";
			$src = $path.$tmpl['pic_big'];
			$thumb = $path."thumb_".$tmpl['pic_big'];		
			?>
	
		<td align="center" style="vertical-align:bottom !important;"><div id="motsp">
                
                    <p><a style="margin:auto;" href="<?php echo $src;?>" rel="lightbox[gallery]" title="<?php echo $tmpl['templateid'];?>"><img border="0" src="<?php echo $thumb;?>" alt="<?php echo $tmpl['templateid'];?>" class="bordered" /></a></p>
                    <p><?php echo $tmpl['templateid']; ?></p>
              </div></td>
	              <?php
		    $tmpl = $DB->fetch_row($result);
		    if (!isset($nested_tmpl)) {
		      $nested_tmpl= 1;
		    }
		    if (isset($tmpl) && is_array($tmpl) && $nested_tmpl++ % 5==0) {
		      echo "</tr><tr>";
		    }
	
		 }
	?></tr></table>
<br class="clear">
<?=$pagination?>
</div>
